package com.zxx.pattern.observerPattern.observer1;

import java.util.ArrayList;
import java.util.List;

/**
 * 被观察者的具体类：报社类
 *
 * @author zxx on 2021/3/9
 */
public class NewsOffice extends Subject {
    private List<Observer> observers;

    public NewsOffice() {
        super.name = "报社";
        this.observers = new ArrayList<>();
    }

    @Override
    void addObserver(Observer observer) {
        this.observers.add(observer);
    }

    @Override
    void removeObserver(Observer observer) {
        int index = this.observers.indexOf(observer);
        if (index >= 0) {
            this.observers.remove(observer);
        }
    }

    @Override
    void notifyObservers() {
        if (this.observers.size() > 0) {
            for (Observer observer : observers) {
                observer.update(super.thing);
                observer.display();
            }
        }
    }
}
